Or alternately, we can use a negative sign when subsetting to get similar results (provided we know the index)
x[-2]# [1] 0 6 9 12 15
We can use the which() command to find the indices of values that meet a given condition.
which(x ==15)# [1] 6
x[which(x ==15)]# [1] 15
Another useful, but rarely documented comparison is %in%, which is a logical comparison for whether the items in the object on the left are found in the object on the right:
z <-c(10:20)# one vector of numbers
z
# [1] 10 11 12 13 14 15 16 17 18 19 20
x <-seq(0, 15, by=3)# another
x
# [1] 0 3 6 9 12 15
x %in% z # like asking, "is this value of x in vector z?"# [1] FALSE FALSE FALSE FALSE TRUE TRUE
x[x %in% z]# [1] 12 15
subset() can be useful for subsetting your data according to a condition or conditions
subset(df, color ="brunette) # I just want the subset of the "df" data frame in which color is equal to brunette.
subset(df, style = "braid", select = c("color", "style", "length") ) # You can select which columns of the data frame you want to extract, too
loops (or not)
(Note: R is inefficient with this kind of logic or looping structure. It is _much_ better if you can do the same thing by working vectors. Often this can be done with some variant of the apply() functions. There is a nice, illustrative walk-through at http://nsaunders.wordpress.com/2010/08/20/a-brief-introduction-to-apply-in-r/)
To cycle through a bit of code over and over, we can use a for() loop. In this (admittedly trivial) example, each value of y depends on the y-value before it:
y <-numeric(10)
y[1]<-4for(i in2:10){# i is the index. it cycles from 2 to 3 to 4 to ... 20
y[i]<- y[i-1]+3# take the previous entry (based on the index, minus 1) and add three to that value}
y
# [1] 4 7 10 13 16 19 22 25 28 31
A more intuitive for loop? I've been trying to model population growth over time using a simple leslie matrix format. For some reason, this was difficult for me to do with a for() loop, but I found the while() loop to be much more intuitive. I'm not sure what the differences are between them, but I guess while loops work more like for loops do in other programming languages. Here is a simplified version of what I did. mat is the leslie matrix, pop is the initial population vector. cbind() is also this nifty thing that concatenates the matrices. So if you have trouble with the for loop (like me), maybe you could try the while loop.
logical comparisons & subsetting
You already know that == means "equals," but did you know that != means "not equal?"Or alternately, we can use a negative sign when subsetting to get similar results (provided we know the index)
We can use the which() command to find the indices of values that meet a given condition.
Another useful, but rarely documented comparison is %in%, which is a logical comparison for whether the items in the object on the left are found in the object on the right:
subset() can be useful for subsetting your data according to a condition or conditionsloops (or not)
(Note: R is inefficient with this kind of logic or looping structure. It is _much_ better if you can do the same thing by working vectors. Often this can be done with some variant of the apply() functions. There is a nice, illustrative walk-through at http://nsaunders.wordpress.com/2010/08/20/a-brief-introduction-to-apply-in-r/)To cycle through a bit of code over and over, we can use a for() loop. In this (admittedly trivial) example, each value of y depends on the y-value before it:
A more intuitive for loop? I've been trying to model population growth over time using a simple leslie matrix format. For some reason, this was difficult for me to do with a for() loop, but I found the while() loop to be much more intuitive. I'm not sure what the differences are between them, but I guess while loops work more like for loops do in other programming languages. Here is a simplified version of what I did. mat is the leslie matrix, pop is the initial population vector. cbind() is also this nifty thing that concatenates the matrices. So if you have trouble with the for loop (like me), maybe you could try the while loop.